#pragma once
#include "GameSystem.hpp"
#include "RigidBody.hpp"
#include "Transform.hpp"
#include <list>
#include "Property.hpp"
#include <btBulletDynamicsCommon.h>


namespace Typhoon {
	class PhysicsSystem : public GameSystem
	{
	public:
		PhysicsSystem();
		~PhysicsSystem();

		void Update(float dt);

		Property<PhysicsSystem*, Vector3> Gravity;
		
	protected:
		void GameObjectAdded(GameObject* gameObject);
		void GameObjectRemoved(GameObject* gameObject);

	private:

		btCollisionConfiguration* collisionConfiguration;
		btDispatcher* dispatcher;
		btBroadphaseInterface* broadphase;
		btConstraintSolver* solver;
		btDynamicsWorld* world;

		//btDefaultCollisionConfiguration m_collisionConfiguration;
		//btCollisionDispatcher* m_dispatcher;
		//btDbvtBroadphase* m_broadphase;
		//btSequentialImpulseConstraintSolver* m_solver;

		

		typedef std::list<btMotionState*> MotionStates;
		MotionStates motionStates;
		
		typedef std::list<btRigidBody*> Bodies;
		Bodies bodies;

		void TransformChanged(Transform* transform);

		bool isSimulating;

		void GravityChanged(PhysicsSystem* physicsSystem);

		void ImpulseReceived(RigidBody::ImpulseEvent& impulse);

		
		class JointSystem : public GameSystem {
			friend class PhysicsSystem;
		public:
			JointSystem();

			btDynamicsWorld* world;
			PhysicsSystem* physics;

		protected:
			void GameObjectAdded(GameObject* gameObject);
			void GameObjectRemoved(GameObject* gameObject);
		private:
			
		};


		JointSystem joints;












	};
}

